#ifndef FILEDRAWWIDGET_H
#define FILEDRAWWIDGET_H

//绘图相关
#include"glrenderer.h"
#include<QOpenGLWidget>
#include<QOpenGLFunctions>
#include<QOpenGLPaintDevice>
#include<GL/glu.h>
//鼠标事件
#include<QMouseEvent>
#include<QApplication>
//投影
#include"projectionlambert.h"
//索引
#include"gridindex.h"
#include"quadtreeindex.h"

enum mouseStat {NONE,MOVE,SCALE,SELECT};

class fileDrawWidget:public QOpenGLWidget , protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit fileDrawWidget(QWidget  *parent=nullptr);
    ~fileDrawWidget() override;
    geoMap map;


    glRenderer openglRenderer;
    void getMinMax(geoMap *map);//获取绘图文件的坐标极值
    void getProjMinMax(geoMap *map);//获取投影坐标极值
    QPointF screenToOpenGL(QPointF pt);//将屏幕坐标转换为OpenGL世界坐标
    QPointF openGLToProjected(QPointF pt);//将OpenGL世界坐标转换为地图投影后坐标
    mouseStat mouseStatus;//鼠标状态定义
    int width,height;//窗口长宽
    bool isIndexSelected;//是否选择了操作索引
    double maxX=-99999999,maxY=-99999999,minX=99999999,minY=99999999;//经纬度坐标的极值
    double projMaxX=-99999999,projMaxY=-99999999,projMinX=99999999,projMinY=99999999;//投影坐标的极值

    //索引相关
    bool setCurrentIndex(spatialIndex *index);//设置当前使用的投影
    gridIndex* getGridIndex();
    quadTreeIndex* getQuadTreeIndex();

    //鼠标事件
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev)override;
    //滚轮事件
    void wheelEvent(QWheelEvent *ev)override;
    //复位
    void resetMap();
    //调整窗口大小
    double getWidth();
    double getHeight();

    void readSLD(QString pathName);//读取sld文件

    void drawSelectRec();//绘制选择框


public slots:
    void paintGL() override;
    void initializeGL() override;          //初始化设置
    void resizeGL(int w, int h) override;  //窗口尺寸变化响应函数
signals:
    void mapLoaded(geoMap* map1);
protected:


private:
    bool reset;
    GLdouble projectionMatrix[16];
    projectionLambert *lambert;

    double offX,offY,preOffX,preOffY;//坐标偏移量
    double scale,preScale;//缩放系数
    QPointF cursorPreLoc;//鼠标原位置
    QPointF recSelectStart,recSelectEnd;//选择点，可用于保存框选的起始点
    gridIndex* indexGrid;
    quadTreeIndex* indexQuadTree;
    spatialIndex* currentIndex;//当前操作索引
};

#endif // FILEDRAWWIDGET_H
